//
//  AddAddressViewController.m
//  CommunityApp
//
//  Created by 秦乾龙 on 16/12/16.
//  Copyright © 2016年 秦乾龙. All rights reserved.
//

#import "AddAddressViewController.h"
#import "AddAddressViewController.h"
#import "AFNetworking.h"//主要用于网络请求方法
#import "UIKit+AFNetworking.h"//里面有异步加载图片的方法
#import "AFHTTPSessionManager.h"
#import "UIButton+WebCache.h"
#import "UIImageView+WebCache.h"
#import "NavView.h"
#import "ZYButton.h"
#import "AddressPickerView.h"
#import "Address.h"
#define kScreenW [UIScreen mainScreen].bounds.size.width
#define kScreenH [UIScreen mainScreen].bounds.size.height
#define kScale   ([UIScreen mainScreen].bounds.size.width)/750
@interface AddAddressViewController ()<UITextFieldDelegate,UITextViewDelegate,AddressPickerViewDelegate>
@property (nonatomic,strong) NSArray *dataAray;
@property (nonatomic,strong) UITextView *detailView2;
@property (nonatomic,strong) UILabel *placeholderLable;
@property (nonatomic,strong) AddressPickerView * pickerView;
@property (nonatomic,retain) NSDictionary *allDic;
@property (nonatomic,retain) UILabel *addLable;
@property (nonatomic,retain) UIButton *selectAddressBtn;
@property (nonatomic,retain) UITextField *phoneField;
@property (nonatomic,retain) UITextField *nameField;
@property (nonatomic,retain) NSString *provinceID;
@property (nonatomic,retain) NSString *cityID;
@property (nonatomic,retain) NSString *areaID;
@property (nonatomic,assign) BOOL  isDefault;

@end

@implementation AddAddressViewController
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationController.navigationBar.userInteractionEnabled = YES;
    self.navigationItem.titleView.userInteractionEnabled = YES;
    if (_isAdd||[_address.is_default isEqualToString:@"0"])
    {
        _isDefault = NO;
    }
    else
    {
        _isDefault = YES;
    }
    
    
    _provinceID = _address.province_id;
    _cityID = _address.city_id;
    _areaID = _address.area_id;
    self.view.exclusiveTouch = NO;
    
    
    [self setUI];
}
- (void)setUI
{
    NSString *title = nil;
    if (_isAdd == YES)
    {
        title = @"添加收货地址";
    }
    else
    {
        title = @"修改收货地址";
    }
    self.navigationItem.titleView = [ViewFactory makeNavigationTitle:title];
    
    UIBarButtonItem *backBtn = [ViewFactory makeNavigationBackWhite:self action:@selector(backClick)];
    self.navigationItem.leftBarButtonItem = backBtn;
   ;
    UIButton *buttonNotice = [UIButton buttonWithType:UIButtonTypeCustom];
    buttonNotice.frame = CGRectMake(0, 0, 40, 20);
    buttonNotice.titleLabel.font = [UIFont systemFontOfSize:15];
    [buttonNotice setTitle:@"确定" forState:UIControlStateNormal];
    
    UIBarButtonItem *btnNoticeItem = [[UIBarButtonItem alloc]initWithCustomView:buttonNotice];
    [buttonNotice addTarget:self action:@selector(changeClick) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.rightBarButtonItem = btnNoticeItem;
    self.view.backgroundColor = [UIColor colorWithRed:32/255.0 green:32/255.0 blue:32/255.0 alpha:1.0f];
        UIView *backgroundView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kScreenW, kScreenH)];
    backgroundView.backgroundColor = [UIColor colorWithRed:245/255.0 green:245/255.0 blue:245/255.0 alpha:1.0f];
    [self.view addSubview:backgroundView];
    UIView *nameView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kScreenW, 100*kScale)];
    nameView.backgroundColor = [UIColor whiteColor];
    [backgroundView addSubview:nameView];
    _nameField = [[UITextField alloc]init];
    _nameField.delegate = self;
    _nameField.font = [UIFont systemFontOfSize:15];
    _nameField.placeholder = @"请输入收货人姓名";
    if (!_isAdd) {
        _nameField.text = _address.name;
    }
    _nameField.frame = CGRectMake(30*kScale, 0, kScreenW, 100*kScale);
    [nameView addSubview:_nameField];
    UIView *phoneView = [[UIView alloc]initWithFrame:CGRectMake(0, 102*kScale, kScreenW, 100*kScale)];
    phoneView.backgroundColor = [UIColor whiteColor];
    [backgroundView addSubview:phoneView];
    _phoneField = [[UITextField alloc]init];
    _phoneField.delegate = self;
    _phoneField.font = [UIFont systemFontOfSize:15];
    _phoneField.keyboardType = UIKeyboardTypePhonePad;
    _phoneField.placeholder = @"请输入手机号码";
    if (!_isAdd) {
        _phoneField.text = _address.mobile;
    }
    _phoneField.frame = CGRectMake(30*kScale, 0, kScreenW, 100*kScale);
    [phoneView addSubview:_phoneField];
    
    UIView *addressView = [[UIView alloc]initWithFrame:CGRectMake(0, 204*kScale, kScreenW, 100*kScale)];
    addressView.backgroundColor = [UIColor whiteColor];
    [backgroundView addSubview:addressView];
    UILabel *addressLable = [[UILabel alloc]init];
    addressLable.font = [UIFont systemFontOfSize:15];
    
    addressLable.text = @"所在地区";
    addressLable.frame = CGRectMake(30*kScale, 0, kScreenW, 100*kScale);
    [addressView addSubview:addressLable];
    
    _addLable = [[UILabel alloc]init];
    _addLable.font = [UIFont systemFontOfSize:15];
    
    _addLable.text = @"请选择";
    if (!_isAdd) {
        _addLable.text = [NSString stringWithFormat:@"%@%@%@",_address.province_name,_address.city_name,_address.area_name];
    }
    _addLable.textAlignment = NSTextAlignmentRight;
    _addLable.frame = CGRectMake(30*kScale, 0, kScreenW-102*kScale-20*kScale, 100*kScale);
    [addressView addSubview:_addLable];
    UIImageView *btnImg = [[UIImageView alloc]init];;
    btnImg.frame = CGRectMake(kScreenW-30*kScale-42*kScale, 29*kScale, 42*kScale, 42*kScale);
    btnImg.image = [UIImage imageNamed:@"右边"];
    [addressView addSubview:btnImg];
    _selectAddressBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    _selectAddressBtn.frame = addressView.frame;
    _selectAddressBtn.userInteractionEnabled = YES;
    [_selectAddressBtn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
    [backgroundView addSubview:_selectAddressBtn];
    UIView *detailView = [[UIView alloc]initWithFrame:CGRectMake(0, 306*kScale, kScreenW, 280*kScale)];
    detailView.backgroundColor = [UIColor whiteColor];
    [backgroundView addSubview:detailView];
    _detailView2 = [[UITextView alloc]init];
    _detailView2.frame = CGRectMake(26*kScale, 306*kScale, kScreenW, 280*kScale);
    _detailView2.delegate = self;
    _detailView2.font = [UIFont systemFontOfSize:15];
    if (!_isAdd) {
        _detailView2.text = _address.address_info;
    }
    [backgroundView addSubview:_detailView2];
    _placeholderLable = [[UILabel alloc]init];
    _placeholderLable.text = @"请填写详细地址";
    _placeholderLable.textColor = [UIColor colorWithRed:158/255.0 green:158/255.0 blue:158/255.0 alpha:1.0f];
    _placeholderLable.font = [UIFont systemFontOfSize:15];
    _placeholderLable.frame = CGRectMake(4*kScale, 0, kScreenW-60*kScale, 70*kScale);
    if (_detailView2.text.length>0) {
        _placeholderLable.hidden = YES;
    }
    [_detailView2 addSubview:_placeholderLable];
    UIView *setView = [[UIView alloc]initWithFrame:CGRectMake(0, (306+280+20)*kScale, kScreenW, 100*kScale)];
    setView.backgroundColor = [UIColor whiteColor];
    [backgroundView addSubview:setView];
    UILabel *setLable = [[UILabel alloc]init];
    setLable.font = [UIFont systemFontOfSize:15];
    
    setLable.text = @"设置为默认";
    setLable.frame = CGRectMake(30*kScale, 0, kScreenW, 100*kScale);
    [setView addSubview:setLable];
    
    UISwitch *switchButton = [[UISwitch alloc] initWithFrame:CGRectMake(kScreenW-60*kScale-70*kScale, 29*kScale, 70*kScale, 42*kScale)];
    switchButton.center = CGPointMake(kScreenW-30*kScale-(switchButton.frame.size.width)/2, 50*kScale);
    if (_isDefault == NO||_isAdd)
    {
        [switchButton setOn:NO];
    }
    else
    {
        [switchButton setOn:YES];
    }
    [switchButton addTarget:self action:@selector(switchAction:) forControlEvents:UIControlEventValueChanged];
    [setView addSubview:switchButton];
    [self.view addSubview:self.pickerView];
    
}
- (void)request {
    DYLog(@"1=%@,2＝%@,3=%@,4=%@,5=%@,6=%@,7=%d",_provinceID,_cityID,_areaID,_detailView2.text,_nameField.text,_phoneField.text,_isDefault);
    if (_provinceID&&_cityID&&_areaID&&_detailView2.text&&_nameField.text&&_phoneField.text)
    {
        NSString *urlStr = nil;
        if (_isAdd) {
            urlStr = [[NSString stringWithFormat:@"%@/userCenter/Xzaddress?province_id=%@&city_id=%@&area_id=%@&address_info=%@&name=%@&mobile=%@&is_default=%d",APIYIGOU,_provinceID,_cityID,_areaID,_detailView2.text,_nameField.text,_phoneField.text,_isDefault] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
        }
        
        else
        {
            urlStr = [[NSString stringWithFormat:@"%@/userCenter/Xzaddress?address_id=%@&province_id=%@&city_id=%@&area_id=%@&address_info=%@&name=%@&mobile=%@&is_default=%d",APIYIGOU,_address.address_id,_provinceID,_cityID,_areaID,_detailView2.text,_nameField.text,_phoneField.text,_isDefault] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
        }
        DYBaseRequest *request = [DYBaseRequest new];
        request.requestUrl = urlStr;
        [request dyBgStartWithCompletionBlockWithSuccess:^(YTKBaseRequest *request) {
            _allDic = [NSDictionary changeType:request.responseJSONObject];
            NSLog(@"77777%@",_allDic);
            if (_isAdd) {
                [ViewFactory showSuccess:@"添加成功"];
            }
            else
            {
                [ViewFactory showSuccess:@"修改成功"];
            }
            [self.navigationController popViewControllerAnimated:YES];
        } error:^(YTKBaseRequest *request) {
            _allDic = [NSDictionary changeType:request.responseJSONObject];
            
            [ViewFactory showInfoMsg:_allDic[@"message"]];
           
        } failure:^(YTKBaseRequest *request) {
            [ViewFactory showErrorStrMsg:NETWORK_ERR];
        }];
    }
    else
    {
        [ViewFactory showInfoMsg:@"请输入完整信息"];
    }
}
- (AddressPickerView *)pickerView{
    if (!_pickerView) {       
        _pickerView = [[AddressPickerView alloc]initWithFrame:CGRectMake(0,kScreenH, kScreenW, 215)];
        _pickerView.delegate = self;
    }
    return _pickerView;
}
-(void)textViewDidChange:(UITextView*)textView
{
    
    if([_detailView2.text length] == 0){
        
        _placeholderLable.text = @"请填写详细地址";
        
    }else{
        
        _placeholderLable.text = @"";//这里给空
        
    }
}
- (void)btnClick:(UIButton *)btn{
    [UIView animateWithDuration:0.5 animations:^{
        
        [UIView beginAnimations:@"move" context:nil];
        [UIView setAnimationDuration:0.75];
        [UIView setAnimationDelegate:self];
        //改变它的frame的x,y的值
        if (btn.selected) {
            _pickerView.frame = CGRectMake(0, kScreenH, kScreenW, 215);
        }
        else {
            _pickerView.frame=CGRectMake(0,kScreenH - 215, kScreenW,215);
        }
        [UIView commitAnimations];
        
        btn.selected = !btn.selected;
        
    }];
}

#pragma mark - AddressPickerViewDelegate
- (void)cancelBtnClick{
    NSLog(@"点击了取消按钮");
    [self btnClick:nil];
}
- (void)sureBtnClickReturnProvince:(NSString *)province City:(NSString *)city Area:(NSString *)area{
    _addLable.text = [NSString stringWithFormat:@"%@%@%@",province,city,area];
    [self btnClick:_selectAddressBtn];
}
- (void) sureBtnClickReturnProvinceID:(NSString *)provinceID CityID:(NSString *)cityID AreaID:(NSString *)areaID
{
    _provinceID = provinceID;
    _cityID = cityID;
    _areaID = areaID;
}

-(void)switchAction:(id)sender
{
    UISwitch *switchButton = (UISwitch*)sender;
    BOOL isButtonOn = [switchButton isOn];
    if (isButtonOn) {
        _isDefault = 1;
    }else {
        _isDefault = 0;
    }
}
//键盘弹下去
- (void) touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [_nameField resignFirstResponder];
    [_phoneField resignFirstResponder];
    [_detailView2 resignFirstResponder];
//    self.view.frame = CGRectMake(0.0f, 0.0f, self.view.frame.size.width, self.view.frame.size.height);
}

- (BOOL) textFieldShouldBeginEditing:(UITextField *)textField
{
    
    
    
    return YES;
}
- (BOOL) textFieldShouldEndEditing:(UITextField *)textField
{
    return YES;
}
- (void)backClick
{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)changeClick
{
    [self request];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
